#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fileencoding=utf-8
# vim:tabstop=4
#
# copyright (c) 2010 by The Codedrawer Team
#
# 成员
# xkhhdx(HD.Huo)	viking9345(C.Cai)	renxing225(X.Ren)
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# 代码来自：http://code.google.com/p/codedrawer/
#
#--- 更新记录 ---
#成员		时间			简述
#xkhhdx		17:04 2010-7-20 增加了遍历文件的功能
#xkhhdx		16:32 2010-7-20 添加了测试代码模块
#
#--- 计划列表 ---
#提出者		时间			是否解决	解决者	计划简述		解决简述
#

import core
from core.cpp_code_render import *
from core.java_code_render import *
from core.python_code_render import *
from core.php_code_render import *

import os, re

def render_file(arg, dirname, names):
    spliter = re.compile(r'(.*)_code\.txt$') # 用正则一块把文件名和语言都捡出来了
    for name in names:
        if spliter.search(name.lower()):
            filename = dirname + "\\" + name; lang = spliter.search(name.lower()).group(1)
            
            print u"文件名：", filename
            print "========================="
            
            # 读文件
            try:
                file_object = file(filename)
                text = file_object.read()
            except IOError:
                print u"文件打开失败！x_x..."
            else:
                # 开始渲染
                cr = eval(lang + "_code_render(%s)" % arg["style"])
                cr.code_buffer = text
                cr.render()
                
                # 关闭文件
                file_object.close()
                
                print u"原始代码："
                print text
                print "-------------------------"
                print u"标记代码："
                print cr.rendered_code_buffer
                print ">>>>>>>>>>>>>>>>>>>>>>>>>"
                
                cr=None

if __name__ == "__main__":
    print ">>>>>>>>>>>>>>>>>>>>>>>>>"
    print u"标记说明："
    print ur'''[关键字]    {"字符串"}    <注释>'''
    print u"\t作者：xkhhdx"
    print ">>>>>>>>>>>>>>>>>>>>>>>>>"
    
    os.path.walk( ".", render_file, ({"style":"'html'"}))
    os.path.walk( ".", render_file, ({"style":""}))
